package com.lovo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.system.constant.MessageCode;
import com.lovo.system.dto.PageDto;
import com.lovo.system.dto.UserDto;
import com.lovo.system.dto.UserRoleDto;
import com.lovo.system.dto.login.LoginDto;
import com.lovo.system.service.IRoleService;
import com.lovo.system.service.IUserRoleService;
import com.lovo.system.service.IUserService;
import com.lovo.system.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    IUserRoleService userRoleService;

    @Autowired
    IRoleService roleService;

    @RequestMapping("/info")
    public IPage<UserDto> info(@RequestBody PageDto<UserDto> pageDto) {
        return userService.cutByItem(pageDto.getCurPage(), pageDto.getPageSize(), pageDto.getData().getUserName());
    }

    @RequestMapping("/selectOne")
    public UserDto selectOne(int id) {
        return userService.selectOne(id);
    }

    @RequestMapping("/update")
    @Transactional
    public ResponseResult updateUser(int[] roleIds,int userId){
        ResponseResult responseResult = new ResponseResult();
        userRoleService.deleteUserRoleByUser(userId);
        for (int roleId : roleIds){
            UserRoleDto userRoleDto = new UserRoleDto();
            userRoleDto.setUserId(userId);
            userRoleDto.setRoleId(roleId);
            userRoleService.insertUserRole(userRoleDto);
        }
        return responseResult;
    }

    @RequestMapping("/add")
    @Transactional
    public ResponseResult insertUser(String userName,String realName,int[] roleIds) {
        ResponseResult responseResult = new ResponseResult();
        UserDto userDto = new UserDto(userName,"123456",realName);
        int userId = userService.insertUser(userDto);
        for (int roleId : roleIds){
            UserRoleDto userRoleDto = new UserRoleDto();
            userRoleDto.setUserId(userId);
            userRoleDto.setRoleId(roleId);
            userRoleService.insertUserRole(userRoleDto);
        }
        return responseResult;
    }

    @RequestMapping("/delete")
    @Transactional
    public ResponseResult deleteUser(int id) {
        ResponseResult responseResult = new ResponseResult();
        userService.deleteUser(id);
        userRoleService.deleteUserRoleByUser(id);
        return responseResult;
    }

    @RequestMapping("/updateUserPassWord")
    public String updateUserPassWord(String password, HttpServletRequest request) {
        return userService.updateUserPassWord((Integer) request.getSession().getAttribute("userId"),password);
    }

    @RequestMapping("/selectPassword")
    public String selectPassword(HttpServletRequest request){
        return userService.selectPassword((Integer) request.getSession().getAttribute("userId"));
    }

    @RequestMapping("/login")
    @Transactional
    public ResponseResult<LoginDto> login(String userName, String passWord, HttpServletRequest request){
        ResponseResult<LoginDto> responseResult = new ResponseResult<>();
        UserDto userDtoLogin = new UserDto(userName,passWord);
        if (userService.selectLogin(userDtoLogin)>0){
            UserDto userDto = userService.selectUser(userDtoLogin);
            List<Integer> listRoleIds = roleService.selectUserRoleNow(userDto.getId());
            List<Integer> listPerIds = roleService.selectRolePerIds(listRoleIds);
            LoginDto loginDto = new LoginDto();
            loginDto.setUserId(userDto.getId());
            loginDto.setPermissions(listPerIds);
            request.getSession().setAttribute("userId",userDto.getId());
            request.getSession().setAttribute("realName",userDto.getRealName());
            responseResult.setData(loginDto);
            return responseResult;
        }
        responseResult.setCode("000001");
        responseResult.setMessage("账号或密码错误");
        return responseResult;
    }

}
